home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15454 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  35 lines

  1. Newsgroups: comp.lang.c,comp.unix.programmer
  2. Path: howland.reston.ans.net!torn!sq!msb
  3. From: msb@sq.com (Mark Brader)
  4. Subject: Re: Q: '\n' character
  5. Message-ID: <1996Apr19.013148.18227@sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <4kj66f$k0o@ren.cei.net> <829396473snz@genesis.demon.co.uk> <4kpd2g$eeb@masala.cc.uh.edu> <4l3ta3$c08@uhura.phoenix.net>
  8. Date: Fri, 19 Apr 1996 01:31:48 GMT
  9.  
  10. > >  ptr = strchr (buffer, '\n');   /* or strrchr() */
  11. > >  if (ptr) *ptr = '\0';
  12. > Which is a lot of code when this will do the same thing:
  13. >    strtok( ptr, "\n");
  14.  
  15. I think this was meant to be
  16.  
  17.     strtok( buffer, "\n");
  18.  
  19. rather than introducing the new variable ptr which is needed in the first
  20. version.  But anyway, it's wrong.  If the FIRST character in the buffer is
  21. a newline, this strtok() call will say "I found no newline-delimited tokens"
  22. by returning a null pointer, and will leave the buffer alone.
  23.  
  24. Therefore, if you try to use this to strip from the buffer the newline that
  25. fgets() left in it, you will end up with "\n" rather than "" in the buffer
  26. every time that a empty line was read from the input.
  27.  
  28. -- 
  29. Mark Brader             "You wake me up early in the morning to tell me I am
  30. msb@sq.com               right?  Please wait until I am wrong."
  31. SoftQuad Inc., Toronto        -- John von Neumann, on being phoned at 10 a.m.
  32.  
  33. My text in this article is in the public domain.
  34.